<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['manager']==1){
		$serveids=pdo_getall('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'managerid'=>$_SESSION['uid']),'id','id');
		$serveids=array_keys($serveids);
		$params=array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$serveids);
		if(isset($_RQ['status']) && $_RQ['status'] != 10){
			$params['status']=intval($_RQ['status']);
			$return['params']['status']=$_RQ['status'];
		}else{
			$return['params']['status']=10;
		}
		$order_list=pdo_getslice('jy_yuyue_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	}else{
		//查询员工的订单
		$worker_id=pdo_getcolumn('jy_yuyue_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
		$params=array('worker_id'=>$worker_id,'uniacid'=>$_SESSION['uniacid']);
		if(isset($_RQ['status']) && $_RQ['status'] != 10){
			$params['status']=intval($_RQ['status']);
			$return['params']['status']=$_RQ['status'];
		}else{
			$return['params']['status']=10;
		}
		$order_list=pdo_getslice('jy_yuyue_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	}
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('jy_yuyue_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$serve=pdo_get('jy_yuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$user['name'];
			$item['user_mobile']=$user['mobile'];
			$item['serve_title']=$serve['title'];
			$item['serve_image']=$serve['image'];
			$item['package']=json_decode($item['package']);
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$serve_times=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$serve_times[$item['timeid']];
			
		}
	}
	$return['list']=$order_list;
	$return['params']['page'] =$pindex;
	exi($return);
}
//今日订单
if($_RQ['op']=='today'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['manager']==1){
		$serveids=pdo_getall('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'managerid'=>$_SESSION['uid']),'id','id');
		$serveids=array_keys($serveids);
		$params=array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$serveids);
		$params['day']=date("Y-m-d",time("day"));	
		if(isset($_RQ['status']) && $_RQ['status'] != 10){
			$params['status']=intval($_RQ['status']);
			$return['status']=$_RQ['status'];
		}
		$order_list=pdo_getslice('jy_yuyue_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	}else{
		$worker_id=pdo_getcolumn('jy_yuyue_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
		$params=array('worker_id'=>$worker_id,'uniacid'=>$_SESSION['uniacid']);
		if(isset($_RQ['status'])){
			$params['status']=intval($_RQ['status']);
			$return['status']=$_RQ['status'];
		}
		$params['day']=date("Y-m-d",time("day"));	
		$order_list=pdo_getslice('jy_yuyue_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	}
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('jy_yuyue_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$serve=pdo_get('jy_yuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$user['name'];
			$item['user_mobile']=$user['mobile'];
			$item['serve_title']=$serve['title'];
			$item['serve_image']=$serve['image'];
			$item['package']=json_decode($item['package']);
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$serve_times=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$serve_times[$item['timeid']];
			
		}
	}
	$return['list']=$order_list;
	$return['params']['page'] =$pindex;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order['package']=json_decode($order['package']);
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	$user=pdo_get('jy_yuyue_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	//项目信息
	$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));	
	$serve_times=json_decode($serve['serve_time'],true);
	$order['serve_time']=$serve_times[$order['timeid']];
	//自定义字段数据
	$fileds=$order['fileds']?json_decode($order['fileds'],true):array();
	foreach($fileds as &$item){
	    if($item['data'] && $item['type']==5){
			$item['data']=rtrim($item['data'], ";");
			$item['data']=ltrim($item['data'], ";");
		}
	}
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['serve']=$serve;
	$return['fileds']=$fileds;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	//pdo_update('jy_yuyue_order',array('status'=>'2'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$message_tpl=$this->plugin_setting('message_tpl');
	if($message_tpl['confirm']==1){
		$this->model('notice')->notice_touser($_RQ['id']);
	}
	pdo_update('jy_yuyue_order',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('接收服务成功');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_yuyue_order',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$message_tpl=$this->plugin_setting('message_tpl');
	if($message_tpl['cancel']==1){
		$this->model('notice')->notice_touser($_RQ['id'],'预约被取消通知');

	}
	exi('取消服务成功');
}
if($_RQ['op']=='complete'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_yuyue_order',array('status'=>'4'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//确认订单后，计算员工佣金,存入佣金日志表，并更新员工表员工总佣金
	$worker_id=pdo_getcolumn('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'worker_id');
	$pay=pdo_getcolumn('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'pay');
	$lev_id=pdo_getcolumn('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'lev');
	$rate=pdo_getcolumn('jy_yuyue_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lev_id),'rate');
	if($rate){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'worker_id'=>$worker_id,
			'order_id'=>$_RQ['id'],
			'money'=>$pay*$rate/100,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_yuyue_commission_log',$insert);
		$commission=pdo_getcolumn('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'commission');
		$commission=$commission+$pay*$rate/100;
		pdo_update('jy_yuyue_worker',array('commission'=>$commission),array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id));
	}	
	exi('确认完成服务成功！');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}
if($_RQ['op']=='payinfo'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	$order=pdo_get('jy_yuyue_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item=pdo_get('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'支付详情',
		'look'=>array(
			array('name'=>'tid','title'=>'支付单号','type'=>'string'),
			array('name'=>'status','title'=>'支付状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'pay_type','title'=>'支付方式','type'=>'radio','options'=>'pay_types'),
			array('name'=>'createtime','title'=>'发起支付时间','type'=>'time'),
			array('name'=>'fee','title'=>'支付金额','type'=>'string'),
			array('name'=>'di_fee','title'=>'抵扣金额','type'=>'string'),
		),
	);
	$item['diyfields']=$item['diyfields']?json_decode($item['diyfields'],true):array();
	foreach($item['diyfields'] as &$v){
			if($v['type']==5 && $v['data']){
				$v['data'] = explode(";", $v['data']);
				foreach($v['data'] as $k=>&$i){
					if(empty($v['data'][$k])){
						unset($v['data'][$k]);
					}
				}
			}
			//$v['type']=$formtypes[$v['type']];
	}
	if($item['diyfields'][0]){
		$return['htmls']['look'][]=array(
			'title'=>'支付内容',
			'type'=>'array',
			'name'=>'diyfields',
			'list'=>array(
				array('type'=>'string','name'=>'title','title'=>'表单标题'),	
				array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
			),
		);
	}
	$return['statuss']=array(
		'0'=>'待支付',
		'1'=>'已支付',
		'2'=>'支付失败'
	);
	$return['pay_types']=get_pay_types();
	$return['item']=$item;
	exi($return);
}